<?php

/**
 * Class Lib_Repair_Adapter_LianTong
 * 联通失联修复
 * 2024/11/06
 */
class Lib_Repair_Adapter_LianTong extends Lib_Repair_Base_Abstract
{
    public static $telX_set_key = 'LT_TELX_KEY';
    /**
    /**
     * 修复接口
     * @param $data
     * @return array
     */
    public function repair($data) {
        try {
            $repairData = $data['repairData'];
            if($repairData){
                $repair_num = count($repairData);
                if($repair_num == 0 ){
                    //没有课修复数据
                    return  [
                        'code' => 400,
                        'message' => 'empty data',
                        'data' => [],
                    ];
                }
                $limit = 1;
                if($repair_num < $limit){
                    $differ_num = $limit - $repair_num;
                    for ($x=1; $x<=$differ_num; $x++) {
                        $repairData[] = [
                            'repairName' => "张三",
                            'sha256idCard' => "63640264849a87c90356129d99ea165e37aa5fabc1fea46906df1a7ca50db492",//hash("sha256", '12345678910')
                        ];
                    }
                }
                $result = Lib_Repair_Implement_LianTong::getInstance()->repair($repairData);
                if($result['code'] != '000' && empty($result['data'])){
                    Func_Log::write('repair',   'liantong',"submit repair -- error ".' responsedata: '. json_encode($result));
                    return  [
                        'code' => $result['code'],
                        'message' => $result['message'],
                        'data' => [],
                    ];
                }
                return  [
                    'code' => 200,
                    'message' => $result['message'],
                    'data' => [
                        'batchNo' => $result['data']['batchId']
                    ],
                ];
                Func_Log::write('repair',   'liantong',"submit repair -- success ".' responsedata: '. json_encode($result));
            }
            return  [
                'code' => 400,
                'message' => 'empty data',
                'data' => [],
            ];
        } catch (Exception $exception) {
            Func_Log::write('repair',   'liantong','loss repair --'.$exception->getMessage().' responsedata: '. json_encode($data));
            return  [
                'code' => 500,
                'message' => $exception->getMessage(),
                'data' => [],
            ];

        }
    }
    /**
     * 发送短信接口
     * @param $data
     * @return array
     */
    public function sendSms($data)
    {

        try {
            if($data){
                if(empty($data['idCard']) || empty($data['batchId']) || empty($data['case_id']) || empty($data['messageCode']) ){
                    return false;
                }

                $insert_data = [
                    'card_id' => $data['idCard'],
                    'case_id' => $data['case_id'],
                    'isp' => 2,
                    'content' => json_encode($data),
                    'send_time' => time(),
                    'send_status' => 1,
                    'sms_code' => $data['messageCode'],
                ];
                $data['messageCode'] = Lib_Repair_Base_LT::$messageCode[$data['messageCode']];
                $result = Lib_Repair_Implement_LianTong::getInstance()->sendMessageData($data);
                if($result['code'] != 000 && empty($result['data'])){
                    $insert_data['send_status'] = 2;
                    Func_Log::write('repair',   'liantong',"send sms -- return error ".' responsedata: '. json_encode($result));
                    LrModule_Repair_SmsLog::getInstance()->singleInsert($insert_data);
                    return false;
                }
            }
            LrModule_Repair_SmsLog::getInstance()->singleInsert($insert_data);
            return true;
        } catch (Exception $exception) {
            Func_Log::write('repair',   'liantong','send sms --'.$exception->getMessage().' responsedata: '. json_encode($data));
            return false;

        }
    }

    /**
     * 获取拨打的号码
     * @param $data
     * @return array
     */
    public function bindTel($data){

        try {
            $result = Lib_Repair_Implement_LianTong::getInstance()->binding($data);
            if($result['code'] != 000 && empty($result['data'])){
                Func_Log::write('repair',   'liantong',"binding -- return error ".' responsedata: '. json_encode($result));
                return ['code'=>$result['code'],'message' => $result['message'] ,'data'=>$result];
            }
            return ['code'=>200,'message' => "success" ,'data'=>['toTel'=>$result['data']['telX']]];
        } catch (Exception $exception) {
            Func_Log::write('repair',   'liantong','loss repair --'.$exception->getMessage().' responsedata: '. json_encode($data));
            return ['code'=>500,'message' => "serve error" ,'data'=>[]];

        }
    }

    /*
     *解除绑定
     *
     */
    public function unbinding($data){
        try {
            $telX = $data['telX'];
            $result = Lib_Repair_Implement_LianTong::getInstance()->unbinding($data);
            if($result['code'] == 311){//联通已经解绑
                Lib_Redis_String::del("bind_".$telX);
                Lib_Redis_Set::sRem(self::$telX_set_key,$telX);
                return ['code'=>200,'message' => "success" ,'data'=>$result['data']];
            }
            if($result['code'] != 000){
                Func_Log::write('repair',   'liantong',"unbinding -- return error ".' responsedata: '. json_encode($result));
                return ['code'=>400,'message' => "中间号解绑失败" ,'data'=>[]];
            }
            Lib_Redis_String::del("bind_".$telX);
            Lib_Redis_Set::sRem(self::$telX_set_key,$telX);
            return ['code'=>200,'message' => "success" ,'data'=>$result['data']];
        } catch (Exception $exception) {
            Func_Log::write('repair',   'liantong','loss repair --'.$exception->getMessage().' responsedata: '. json_encode($data));
            return ['code'=>500,'message' => "serve error" ,'data'=>[]];
        }
    }




}